package asteroids.model.programs.parsing.expressions;

import java.util.List;
import java.util.Map;

import asteroids.model.Ship;
import asteroids.model.programs.parsing.types.*;

/**
 * A class of negated expressions.
 * A negated expression returns the negation of the given operand.
 * 
 * @author Thijs Dieltjens - Wim Konings
 * @version 2.0
 */
public class NegatedExpr extends UnOpExpr {

	public NegatedExpr(Expression expr,int line, int column) {
		super(expr,line, column);
	}

	@Override
	public BooleanLiteral eval(Ship ship, Map<String, Type> globals) {
		if(getOperand().eval(ship, null) instanceof BooleanLiteral){
			BooleanLiteral bt = (BooleanLiteral) getOperand().eval(ship, null);
			return new BooleanLiteral(!(bt.getValue()));
		}
		return new BooleanLiteral();
	}

	@Override
	public  List<String> getTypeCheckErrors(List<String> errors, Map<String, Type> globals){
		errors=(getOperand().getTypeCheckErrors(errors, globals));
		try{
		if(getOperand().eval(null, globals).getClass()!=BooleanLiteral.class){
			errors.add(getLine()+"-"+getColumn()+": The given expression must be of type boolean.");
		}
		}
		catch(NullPointerException e){
			errors.add(getLine()+"-"+getColumn()+": No valid expression to negate");
		}
		return errors;
	}
}
